﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Controls
{
    public class PsMonthCalendar : MonthCalendar
    {
        private Point m_LastClickPosition;
        private long m_LastClickTime;
        private Boolean m_LastClickRaisedDoubleClick;


        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: Add custom paint code here

            // Calling the base class OnPaint
            base.OnPaint(pe);
        }

        /**/
        /// <summary>
        /// ModeChanged Event.
        /// </summary>
        [Browsable(true)]
        [Category("Basic_Event"), Description("鼠标双击事件")]
        public event EventHandler DoubleClick;

        protected override void OnDoubleClick(EventArgs e)
        {
            DoubleClick(this, e);
            base.OnDoubleClick(e);
        }

        bool IsInDoubleClickArea(Point Point1, Point Point2)
        {
            return Math.Abs(Point1.X - Point2.X) <= SystemInformation.DoubleClickSize.Width &&
            Math.Abs(Point1.Y - Point2.Y) <= SystemInformation.DoubleClickSize.Height;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!m_LastClickRaisedDoubleClick && System.DateTime.Now.Ticks - m_LastClickTime <= SystemInformation.DoubleClickTime * 10000 && IsInDoubleClickArea(m_LastClickPosition, Cursor.Position))
                {
                    OnDoubleClick(EventArgs.Empty);
                    m_LastClickRaisedDoubleClick = true;
                }
                else
                {
                    m_LastClickRaisedDoubleClick = false;
                }
                m_LastClickPosition = Cursor.Position;
                m_LastClickTime = System.DateTime.Now.Ticks;

            }
            base.OnMouseDown(e);
        }

    }
}
